package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.HeTongService;
import cn.gson.boot.model.service.HuiKuanService;
import cn.gson.boot.model.service.TuiHuoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/huikuan")
public class HuiKuanController {
    @Autowired
    HuiKuanService huiKuanService;
    @Autowired
    HeTongService heTongService;
    @Autowired
    TuiHuoService tuiHuoService;

    @RequestMapping("/huiKuanCkdingdan")
    @ResponseBody
    public List<Dingdan> huiKuanCkdingdan(Integer kId){
        List<Dingdan> dd = heTongService.huiKuanCkdingdan(kId);
        for (int i=0;i<dd.size();i++){
            //如果订单总额等于订单回款金额，就移除订单
            if(dd.get(i).getDdMoney().equals(dd.get(i).getDdHkmoney())){
                System.err.println("aa:"+dd.get(i).getDdMoney() );
                System.err.println("bb:"+dd.get(i).getDdHkmoney());
                dd.remove(i);
            }
        }
        return dd;
    }

    //新建回款
    @RequestMapping("/addHuiKuan")
    @ResponseBody
    public void addHuiKuan(@RequestBody Huikuan huikuan){
        huiKuanService.addHuiKuan(huikuan);
    }

    //查询所有回款
    @RequestMapping("/selectAllHuiKuan")
    @ResponseBody
    public Map<String,Object> selectAllHuiKuan(String kName, String ddHao, String time, Integer pageNumm,
                                             Integer pageSizee, HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Huikuan> list = huiKuanService.selectAllHuiKuan(kName,ddHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }


    //退款==================================================
    //根据选择的客户查询客户所有的退货单（审核状态为同意，并且已经入库）
    @RequestMapping("/tuiKuanCkthd")
    @ResponseBody
    public List<Tuihuo> tuiKuanCkthd(Integer kId){
        List<Tuihuo> th = tuiHuoService.tuiKuanCkthd(kId);
        for (int i=0;i<th.size();i++){
            //如果应退款等于实退款，就移除退货单
            if(th.get(i).getThYingmoney().equals(th.get(i).getThShimoney())){
                th.remove(i);
            }
        }
        return th;
    }

    //新增退款
    @RequestMapping("/addTuiKuan")
    @ResponseBody
    public void addTuiKuan(@RequestBody Tuikuan tuikuan){
        huiKuanService.addTuiKuan(tuikuan);
    }

    //查询所有退款
    @RequestMapping("/selectAllTuiKaun")
    @ResponseBody
    public Map<String,Object> selectAllTuiKaun(String kName,String thDanHao,String time, Integer pageNumm,
                                               Integer pageSizee,HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Tuikuan> list = huiKuanService.selectAllTuiKaun(kName,thDanHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }

}
